Random File Selector

© 2003, Gordon Sweet

author contact:

gordon@gsweet.fsnet.co.uk

Home

Tip Corner

Plot 3d

Memory Mapped File

Random File Selector

Notes for Beginners

No SoundBlaster Board

PrintInstalled Fonts

Essential Libby

API Drive Info

Begginer Series 6

Newsletter help

Index


RANDOM FILE SELECTOR

This shows how files chosen from any folder by one or more .EXT, can then be automatically selected at random one by one,and used. You will see the list of files is created using details similarly provided by DIR.BAS. For the purpose of this demonstration, Midi and MP3 are played in the background as demonstrated in HOCKEY.BAS, kindly provided by David Drake. Both programs are supplied with LB. The same method shown below can be used to produce a continuous display of Clipart, using jpeg.dll or NviewLib.dll etc.

As the API calls used in HOCKEY.BAS do not detect when the system has finished playing a MID or MP3 file, I have set an arbitrary time limit of 3 minutes ( 180 seconds ) before a new file is played. If you wish to improve on the program, there are many excellent examples of MID and MP3 etc players in the e-book produced by Alyce Watson. Some of these can detect the end of playing a file, but you may need to modify the routines if you wish to play music as an accompaniment such as below, which creates a simple pattern continuously.

You will need to deactivate any screen saver in use, for a non stop display, but I do have a simple routine supplied by others, that simulates pressing the Ctrl key at any interval of your choice. This prevents the screen saver being activated.

by Gordon Sweet


The Source Code


' Designed primarily for 800 x 600 and 1024 x 768 Displays
    nomainwin
    ux = 0 : uy = 0  ' Parameters for displays
    if DisplayWidth > 1000 then ux = 120 : uy = 90
    open "RANDOM MUSIC SELECTOR" for graphics_fs_nsb as #v
    dim dir$(10,10) : dim file$(5001)
    #v "trapclose [quit]; down; font arial 14 bold; fill 0 0 40"
    #v "backcolor 0 0 40; color white; size 2; place ";200 + ux;" ";500 + uy
    factor = 2 ' same as size
    #v "\Select a sample music file from any Folder"
    #v "\All MID or MP3 files in the folder will be"
    #v "\played for 3 minutes selected at random."

[select]
    redim dir$(10,10) : redim file$(5001)
    DefaultDir$ = left$(DefaultDir$,2)+"\" ' START FROM ROOT DIR
    filedialog "Select a Sample File", "*.mid;*.mp3", File$
    if File$ = "" then [quit]

    File$ = upper$(File$)
    sFile$ = noPath$(File$)
    plen = len(File$)-len(sFile$)
    path$ = left$(File$,plen)
    type$ = right$(File$,3) ' IDENTIFY .EXT
    if type$ <> "MID" and type$ <> "MP3" then
        notice "ONLY MID OR MP3 !"
        goto [select]
    end if
    ext$ = "*." + type$
    files path$, ext$, dir$()
    qtyFiles = val(dir$(0, 0))
    if qtyFiles > 5000 then qtyFiles = 5000 ' PREVENT MAX. EXCEEDED
    qtySubDirs = val(dir$(0, 1))
    if qtyFiles = 0 and qtySubDirs = 0 then
        notice "EMPTY DIRECTORY!!"
        goto [select]
    end if
    if qtyFiles = 0 then notice "NO FILES??" : goto [select]

   'reformat the file information
    for x = 1 to qtyFiles
       dir$(x, 1) = right$("        " + dir$(x, 1), 9)
    next x
    for x = 1 to qtyFiles
        file$(x) =  dir$(x, 0)
    next x

    count = qtyFiles
    for n = qtyFiles to 1 step -1
        pick = int(rnd(1)*n) + 1   ' PICK AT RANDOM
        new$ = path$+file$(pick) ' Re-insert path$ for Player etc
        gosub [startMusic]
        tim = time$("seconds") ' TIME MUSIC STARTS

' REMOVE PICKED FILE FROM SELECTION
        for r = pick to count
            file$(r) = file$(r+1)
        next r
        count = count -1
        if pick > count then pick = count
        done = 0
[boxes]
        #v "discard; fill 0 0 40; place 40 20"
        #v "|";new$
        x2 = 420 + ux : y1 = 252 + uy : y2 = 288 + uy
        for x = 384+ux to 145+ux step 0 - factor
            pattern = time$("ms")
            while time$("ms") < pattern + 100
                scan
            wend
            red = int(rnd(1) * 256)
            green = int(rnd(1) * 256)
            blue = int(rnd(1) * 256)
            #v "color ";red;" ";green;" ";blue
            #v "place ";x;" ";y1
            #v "box ";x2;" ";y2
            x2 = x2 + factor
            y1 = y1 - factor
            y2 = y2 + factor
            if time$("seconds") > tim + 180 then
                x = 0 - 1000
                done = 1
            end if ' play music for 3 minutes
        next x
        if done = 0 then goto [boxes]
        gosub [stopMusic]
    next n

    confirm "ALL FILES PLAYED - SELECT MORE?"; q$
    if q$ = "yes" then [select] else [quit]

[startMusic]
    mus$ = GetShortPathName$(new$)
    if type$ = "MP3" then a$="open "+mus$+" type MpegVideo alias fictoplay"
    if type$ = "MID" then a$="open "+mus$+" type sequencer alias fictoplay"

    calldll #winmm,"mciSendStringA",a$ as ptr,_
        i1 as ulong,_
        i2 as long,_
        i3 as long,_
        r as long
    a$="play fictoplay"
    calldll #winmm,"mciSendStringA",_
        a$ as ptr,_
        i1 as ulong,_
        i2 as long,_
        i3 as long,_
        r as long
    return

[stopMusic]
    a$="close fictoplay"
    calldll #winmm,"mciSendStringA",_
        a$ as ptr,_
        i1 as ulong,_
        i2 as long,_
        i3 as long,_
        r as long
    return

[quit]
    gosub [stopMusic]
    close #v
    end

Function GetShortPathName$(lPath$)
    lPath$=lPath$+Chr$(0)
    sPath$=Space$(256)
    lenPath=Len(sPath$)
    CallDLL #kernel32, "GetShortPathNameA",lPath$ As ptr,_
    sPath$ As ptr,lenPath As long,r As long
    GetShortPathName$=Left$(sPath$,r)
    End Function

function noPath$(t$)
    while instr(t$, "\")
    t$ = mid$(t$, 2)
    wend
    noPath$ = t$
    end function



Home

Tip Corner

Plot 3d

Memory Mapped File

Random File Selector

Notes for Beginners

No SoundBlaster Board

PrintInstalled Fonts

Essential Libby

API Drive Info

Begginer Series 6

Newsletter help

Index